package activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.laoxinwen.app.R;

import activity.main.MainActivity;
import common.ServerConfig;
import common.VideoWebChromeClient;
import share.ShareActivity;
import share.ShareDialog;

/**
 * Created by ilioili on 2015/4/2.
 */
public class WebviewActivity extends ShareActivity implements VideoWebChromeClient.VideoFullScreenHandler {

    public static final String TITLE = "Title";
    public static final String URL = "URL";
    private String title;
    @id(R.id.tv_title)
    private TextView tvTitle;
    private String url;
    @id(R.id.webView)
    private WebView webView;
    @id(R.id.topbar)
    private FrameLayout topbar;
    @id(R.id.fullWebViewContainer)
    private FrameLayout fullWebViewContainer;

    private VideoWebChromeClient webChromeClient;
    private Dialog shareDialog;

    public static Intent getIntent(String title, String url, Context context) {
        Intent it = new Intent(context, WebviewActivity.class);
        it.putExtra(URL, url);
        it.putExtra(TITLE, title);
        return it;
    }

    @Override
    protected void onRightSlide() {
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_share:
                shareUrl();
                break;
        }
    }

    private void shareUrl() {
        final String title = this.title;
        final String srcUrl = this.url;
        shareDialog = ShareDialog.getShareDialog(this, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareDialog.dismiss();
                switch (v.getId()) {
                    case R.id.share_qq:
                        shareToQq(title, "", ServerConfig.DEFAULT_SHARE_IMG, srcUrl);
                        break;
                    case R.id.share_qq_zone:
                        shareToQqZone(title, "", ServerConfig.DEFAULT_SHARE_IMG, srcUrl);
                        break;
                    case R.id.share_weibo:
                        shareToWeibo(title, "", ServerConfig.DEFAULT_SHARE_IMG, srcUrl);
                        break;
                    case R.id.share_weixin:
                        shareToWeixin(title, "", ServerConfig.DEFAULT_SHARE_IMG, srcUrl);
                        break;
                    case R.id.share_weixin_friends:
                        shareToWeixinFriendCircle(title, "", ServerConfig.DEFAULT_SHARE_IMG, srcUrl);
                        break;
                    case R.id.share_url:
                        shareCopyUrl(srcUrl);
                        break;
                }
            }
        });
        shareDialog.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        loadView();
        title = getIntent().getStringExtra(TITLE);
        url = getIntent().getStringExtra(URL);
        tvTitle.setText(title);

        webChromeClient = new VideoWebChromeClient(webView, fullWebViewContainer, this);
        webView.setWebChromeClient(webChromeClient);
        WebSettings webSettings = webView.getSettings();
        webView.setVerticalScrollBarEnabled(true);
        webView.setHorizontalScrollBarEnabled(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl(url);
    }

    @Override
    public void onFullScreen() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        topbar.setVisibility(View.GONE);
    }

    @Override
    public void onExitFullScreen() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        topbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onBackPressed() {
        if (webChromeClient.inCustomView()) {
            webChromeClient.onHideCustomView();
        } else if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
    }

    @Override
    public void finish() {
        if(this.isTaskRoot()) {
            Intent it = new Intent(this, MainActivity.class);
            startActivity(it);
        }
        super.finish();
    }
}
